<template>
  <div class="search-item accountSelect icon-select-arrow-down"  :class="{'select-disabled':disabled || selectOpts.disabled}" @click="(!disabled || selectOpts.disabled) && onSelect" >
    <div class="select" :class="classDefaultName || selectOpts.classDefaultName">
      <span class="cur" :class="placeholder" ref="curRef" v-text="placeholder || selectOpts.placeholder"></span>
      <ul class="select-list" v-show="isShow || selectOpts.isShow" ref="listRef">
        <li v-for="(item,index) in options" :key="index" v-if="options">
          <div class="option" @click="select" :value="item.value">{{item.title}}</div>
        </li>
      </ul>
    </div>
    <input type="hidden" :name = name v-if="name">
    <input type="hidden" :name = selectOpts.name v-else>
  </div>
</template>
<script>
export default {
  name: 'custom-select',
  props:{
    selectOpts:{
      type:Object,
      default:{
        name:"",
        disabled:false,
        placeholder:"",
        classDefaultName:"",
        isShow:false
      }
    },
    name:{
      type:String,
      default:''
    },
    placeholder:{
      type:String,
      default:''
    },
    classDefaultName:{
      type:String,
      default:''
    },
    options:{
      type:Array,
      default:[]
    },
    isShow:{
      type:Boolean,
      default:false
    },
    selectFn:{
      type:Function
    },
    onChange:{
      type:Function
    },
    disabled:{
      type:Boolean,
      default:false
    }
  },
  data () {
    return {};
  },
  methods:{
    onSelect () {
      this.isShow = true;
      this.selectOpts.isShow = true;
      let curVal = this.$refs.curRef.getAttribute("value");
      let lis = this.$refs.listRef.getElementsByClassName("option");
      for(let i=0;i<lis.length;i++){
        if(lis[i].getAttribute("value") == curVal){
          lis[i].classList.add("selected");
        }else{
          lis[i].classList.remove("selected");
        }
      }
    },
    select (e) {
      this.isShow = false;
      this.selectOpts.isShow = false;
      this.$refs.curRef.setAttribute("value",e.target.getAttribute("value"));
      this.$refs.curRef.innerText = e.target.innerText;
    }
  },
  mounted () {
    let body = document.querySelector('body');
    body.addEventListener('click',(e)=>{
      if(this.selectOpts.classDefaultName){
        if(this.selectOpts.classDefaultName && e.target.className.indexOf(this.selectOpts.classDefaultName) != -1 && !this.selectOpts.disabled){
          this.isShow = true;
        }else {
          this.isShow = false;
        }
      }else{
        if(this.classDefaultName && e.target.className.indexOf(this.classDefaultName) != -1 && !this.disabled){
          this.isShow = true;
        }else {
          this.isShow = false;
        }
      }
    },false);
  }
}
</script>
<style lang="scss" scoped>
.search-item {
  display: inline-block;
  width: 171px;
  height: 25px;
  line-height: 25px;
  background-color: $c_3;
  color: $c_4;
  background-position: 145px -584px;
  cursor: pointer;
  border: 1px $c_9 solid;
  font-size: 12px;
  &.select-disabled {
    background: $c_17 !important;
  }
  .select {
    position: relative;
    width: 100%;
    height: 25px;
    .select-list {
      width: 100%;
      position: absolute;
      background: $c_3;
      border: 1px $c_9 solid;
      // padding-left: 4px;
      top: 25px;
      z-index: 1;
      li {
        height: 25px;
        .option.selected {
          background: $c_2;
          color:$c_3;
        }
      }
    }
  }
}
</style>

